package com.potenzacd.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.potenzacd.project.mapper.ProductInfoMapper;
import com.potenzacd.project.pojo.*;
import com.potenzacd.project.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Cuidongdong
 * @since 2021-01-26
 */
@Service
public class ProductInfoService extends ServiceImpl<ProductInfoMapper, ProductInfo> implements IProductInfoService {

    //产品信息
    @Autowired
    private IProductInfoService productInfoService;
    //历史产品信息
    @Autowired
    private IHisProductInfoService hisProductInfoService;

    //产品审核信息
    @Autowired
    private IProductApprovalInfoService productApprovalInfoService;

    //历史产品审核信息
    @Autowired
    private IHisProductApprovalInfoService hisProductApprovalInfoService;

    //产品终端类型
    @Autowired
    private IProductInfoTerminalTypeService productInfoTerminalTypeService;

    //历史产品终端类型
    @Autowired
    private IHisProductInfoTerminalTypeService hisProductInfoTerminalTypeService;

    //产品信息通知
    @Autowired
    private IMessageNotificationProductService messageNotificationProductService;


    @Override
    public void save4Draft(ProductInfo productInfo) {
        productInfo.setCreateTime(new Date());
        productInfo.setUpdateTime(new Date());
        productInfo.setStatus(ProductInfo.Status.STATUS_NO_ADD);
        productInfo.setBizFlowStatus(ProductInfo.Status.STATUS_NO_ADD);
        productInfo.setPdInfoReqsStatus(ProductInfo.Status.STATUS_NO_ADD);
        productInfo.setVersion(1);
        this.productInfoService.save(productInfo);
        List<ProductInfoTerminalType> productInfoTerminalTypes = productInfo.getProductInfoTerminalTypes();
        for(ProductInfoTerminalType productInfoTerminalType: productInfoTerminalTypes){
            productInfoTerminalType.setProductInfoCode(productInfo.getCode());
            productInfoTerminalType.setCreateTime(new Date());
            productInfoTerminalType.setUpdateTime(new Date());
        }
        this.productInfoTerminalTypeService.saveBatch(productInfoTerminalTypes);
    }

    @Override
    public void save4Approval(ProductInfo productInfo) {
        //1.保存基本信息
        productInfo.setCreateTime(new Date());
        productInfo.setUpdateTime(new Date());
        //将状态修改为审核中
        productInfo.setStatus(ProductInfo.Status.STATUS_CHANGING_WAIT_APPROVAL);
        this.productInfoService.save(productInfo);
        //终端类型信息
        List<ProductInfoTerminalType> productInfoTerminalTypes = productInfo.getProductInfoTerminalTypes();
        for (ProductInfoTerminalType productInfoTerminalType:productInfoTerminalTypes){
            //挂关系
            productInfoTerminalType.setProductInfoCode(productInfo.getCode());
            productInfoTerminalType.setCreateTime(new Date());
            productInfoTerminalType.setUpdateTime(new Date());
        }
        this.productInfoTerminalTypeService.saveBatch(productInfoTerminalTypes);
        //保存历史基本信息
        HisProductInfo hisProductInfo = new HisProductInfo();
        hisProductInfo.setCreateTime(new Date());
        hisProductInfo.setUpdateTime(new Date());
        BeanUtils.copyProperties(productInfo,hisProductInfo);
        this.hisProductInfoService.save(hisProductInfo);
        //保存历史终端信息
        ArrayList<HisProductInfoTerminalType> types = new ArrayList<>();
        for(ProductInfoTerminalType productInfoTerminalType:productInfoTerminalTypes){
            HisProductInfoTerminalType hisProductInfoTerminalType = new HisProductInfoTerminalType();
            //复制
            BeanUtils.copyProperties(productInfoTerminalType,hisProductInfoTerminalType);
            hisProductInfoTerminalType.setHisProductInfoCode(productInfo.getCode());
            types.add(hisProductInfoTerminalType);
        }
        this.hisProductInfoTerminalTypeService.saveBatch(types);

        //消息通知发送
        MessageNotificationProduct messageNotifiProduct = new MessageNotificationProduct();
        messageNotifiProduct.setSendTime(new Date());
        messageNotifiProduct.setFread(2);//未读
        messageNotifiProduct.setHandle(2);//未处理
        messageNotifiProduct.setType(1);//待审核
        messageNotifiProduct.setKind(1);//基本信息
        String title = productInfo.getName() + "的产品基本信息待审核信息";
        messageNotifiProduct.setTitle(title);
        String content = "领导,您有一个产品需要审核,大致如下:" + "产品名称" + productInfo.getName() + "请您审核";
        messageNotifiProduct.setContent(content);
        messageNotifiProduct.setHisProductInfoCode(hisProductInfo.getCode());
        this.messageNotificationProductService.save(messageNotifiProduct);
    }

    @Override
    public void edit4Draft(ProductInfo productInfo) {
        //1:保存正常的基本信息
        productInfo.setCreateTime(new Date());
        productInfo.setUpdateTime(new Date());
        productInfo.setStatus(ProductInfo.Status.STATUS_DRAFT);

        this.productInfoService.updateById(productInfo);
        QueryWrapper<ProductInfoTerminalType> productInfoTerminalTypeQueryWrapper = new QueryWrapper<>();
        productInfoTerminalTypeQueryWrapper.lambda().eq(ProductInfoTerminalType::getProductInfoCode,productInfo.getCode());
        this.productInfoTerminalTypeService.remove(productInfoTerminalTypeQueryWrapper);
        List<ProductInfoTerminalType> productInfoTerminalTypes = productInfo.getProductInfoTerminalTypes();
        for(ProductInfoTerminalType productInfoTerminalType : productInfoTerminalTypes){
            productInfoTerminalType.setProductInfoCode(productInfo.getCode());
        }
        this.productInfoTerminalTypeService.saveBatch(productInfoTerminalTypes);
    }

    @Override
    public void edit4Approval(ProductInfo productInfo) {
        //1、保存基本信息
        productInfo.setCreateTime(new Date());
        productInfo.setUpdateTime(new Date());
        productInfo.setStatus(ProductInfo.Status.STATUS_WAIT_APPROVAL);
        //录入基本信息
        this.productInfoService.updateById(productInfo);
        //想查到相应的，删除原来的，添加新的
        QueryWrapper<ProductInfoTerminalType> productInfoTerminalTypeQueryWrapper = new QueryWrapper<>();
        productInfoTerminalTypeQueryWrapper.lambda().eq(ProductInfoTerminalType::getProductInfoCode, productInfo.getCode());
        this.productInfoTerminalTypeService.remove(productInfoTerminalTypeQueryWrapper);

        List<ProductInfoTerminalType> productInfoTerminalTypes = productInfo.getProductInfoTerminalTypes();
        for (ProductInfoTerminalType productInfoTerminalType : productInfoTerminalTypes) {
            productInfoTerminalType.setProductInfoCode(productInfo.getCode());
        }
        this.productInfoTerminalTypeService.saveBatch(productInfoTerminalTypes);

        //2.保存历史基本信息
        HisProductInfo hisProductInfo = new HisProductInfo();
        //将历史信息set最新信息
        BeanUtils.copyProperties(productInfo, hisProductInfo);
        //设置版本号
        Integer version = productInfo.getVersion();
        hisProductInfo.setVersion(version+1);
        this.hisProductInfoService.save(hisProductInfo);
        ArrayList<HisProductInfoTerminalType> hisProductInfoTerminalTypes = new ArrayList<>();
        for (ProductInfoTerminalType productInfoTerminalType : productInfoTerminalTypes) {
            HisProductInfoTerminalType hisProductInfoTerminalType = new HisProductInfoTerminalType();
            BeanUtils.copyProperties(productInfoTerminalType, hisProductInfoTerminalType);
            hisProductInfoTerminalType.setHisProductInfoCode(hisProductInfo.getCode());
            hisProductInfoTerminalTypes.add(hisProductInfoTerminalType);//&
        }
        this.hisProductInfoTerminalTypeService.saveBatch(hisProductInfoTerminalTypes);
        //消息通知发送
        MessageNotificationProduct messageNotifiProduct = new MessageNotificationProduct();
        messageNotifiProduct.setSendTime(new Date());
        messageNotifiProduct.setFread(2);//未读
        messageNotifiProduct.setHandle(2);//未处理
        messageNotifiProduct.setType(1);//待审核
        messageNotifiProduct.setKind(1);//基本信息
        String title = productInfo.getName() + "的产品基本信息待审核信息";
        messageNotifiProduct.setTitle(title);
        String content = "领导,您有一个产品需要审核,大致如下:" + "产品名称" + productInfo.getName() + "请您审核";
        messageNotifiProduct.setContent(content);
        messageNotifiProduct.setHisProductInfoCode(hisProductInfo.getCode());
        this.messageNotificationProductService.save(messageNotifiProduct);
    }

    @Override
    public void pass(ProductApprovalInfo productApprovalInfo) {
        //1.保存审核信息
        String productInfoCode = productApprovalInfo.getProductInfoCode();
        productApprovalInfo.setCreateTime(new Date());
        productApprovalInfo.setUpdateTime(new Date());
        productApprovalInfo.setPass(1);//通过
        productApprovalInfo.setType(1);//基本信息
        this.productApprovalInfoService.save(productApprovalInfo);
        //修改审核的基本信息
        //根据要审核完的结果更新基本信息状态
        QueryWrapper<ProductInfo> productInfoQueryWrapper = new QueryWrapper<>();
        productInfoQueryWrapper.lambda().eq(ProductInfo::getCode,productInfoCode);
        ProductInfo productInfoServiceOne = this.productInfoService.getOne(productInfoQueryWrapper);
        productInfoServiceOne.setStatus(ProductInfo.Status.STATUS_APPROVAL_PASS);
        this.productInfoService.updateById(productInfoServiceOne);
        //2.保存历史信息
        HisProductApprovalInfo hisProductApprovalInfo = new HisProductApprovalInfo();
        hisProductApprovalInfo.setCreateTime(new Date());
        hisProductApprovalInfo.setUpdateTime(new Date());
        hisProductApprovalInfo.setHisProductInfoCode(productInfoCode);
        //将一个个的属性复制历史记录当中，
        BeanUtils.copyProperties(productApprovalInfo,hisProductApprovalInfo);
        Integer version1 = productInfoServiceOne.getVersion();
        hisProductApprovalInfo.setVersion(version1);
        this.hisProductApprovalInfoService.save(hisProductApprovalInfo);
        //3.发送消息
        MessageNotificationProduct messageNotifiProduct = new MessageNotificationProduct();
        messageNotifiProduct.setSendTime(new Date());//发送时间
        messageNotifiProduct.setFread(2);//未读
        messageNotifiProduct.setHandle(2);//未处理
        messageNotifiProduct.setType(1);//待审核
        messageNotifiProduct.setKind(1);//基本信息
        String title =productInfoServiceOne.getName()+"的产品基本信息审核信息";
        messageNotifiProduct.setTitle(title);
        String content ="老板，您要审核的产品信息内容："+"产品名"+productInfoServiceOne.getName()+"请审核";
        messageNotifiProduct.setContent(content);
        //产品标识
        messageNotifiProduct.setHisProductInfoCode(productInfoCode);
        this.messageNotificationProductService.save(messageNotifiProduct);
    }

    @Override
    public void notPass(ProductApprovalInfo productApprovalInfo) {
        //添加基本信息（审核）
        String productInfoCode = productApprovalInfo.getProductInfoCode();
        productApprovalInfo.setCreateTime(new Date());
        productApprovalInfo.setUpdateTime(new Date());
        productApprovalInfo.setPass(2);//通过
        productApprovalInfo.setType(1);//基本信息
        productApprovalInfo.setProductInfoCode(productInfoCode);
        this.productApprovalInfoService.save(productApprovalInfo);
        //根据要审核完的结果更新基本信息状态
        QueryWrapper<ProductInfo> productInfoQueryWrapper = new QueryWrapper<>();
        productInfoQueryWrapper.lambda().eq(ProductInfo::getCode,productInfoCode);
        ProductInfo productInfoServiceOne = this.productInfoService.getOne(productInfoQueryWrapper);
        productInfoServiceOne.setStatus(ProductInfo.Status.STATUS_APPROVAL_PASS);
        this.productInfoService.updateById(productInfoServiceOne);
        //2.保存历史审核信息
        HisProductApprovalInfo hisProductApprovalInfo = new HisProductApprovalInfo();
        hisProductApprovalInfo.setCreateTime(new Date());
        hisProductApprovalInfo.setUpdateTime(new Date());
        hisProductApprovalInfo.setHisProductInfoCode(productInfoCode);
        //将一个个的属性复制历史记录当中，
        BeanUtils.copyProperties(productApprovalInfo,hisProductApprovalInfo);
        Integer version1 = productInfoServiceOne.getVersion();
        hisProductApprovalInfo.setVersion(version1);
        this.hisProductApprovalInfoService.save(hisProductApprovalInfo);

        MessageNotificationProduct messageNotifiProduct = new MessageNotificationProduct();
        messageNotifiProduct.setSendTime(new Date());
        messageNotifiProduct.setFread(2);//未读
        messageNotifiProduct.setHandle(2);//未处理
        messageNotifiProduct.setType(1);//待审核
        messageNotifiProduct.setKind(1);//基本信息
        String title = productInfoServiceOne.getName() + "的产品基本信息待审核信息";
        messageNotifiProduct.setTitle(title);
        String content = "领导,您有一个产品需要审核,大致如下:" + "产品名称" + productInfoServiceOne.getName() + "请您审核";
        messageNotifiProduct.setContent(content);
        messageNotifiProduct.setHisProductInfoCode(productInfoCode);
        this.messageNotificationProductService.save(messageNotifiProduct);
    }

    @Override
    public void revise(ProductInfo productInfo) {
        //1、保存基本信息
        productInfo.setCreateTime(new Date());
        productInfo.setUpdateTime(new Date());
        productInfo.setStatus(ProductInfo.Status.STATUS_WAIT_APPROVAL);
        //录入基本信息
        this.productInfoService.updateById(productInfo);
        //想查到相应的，删除原来的，添加新的
        QueryWrapper<ProductInfoTerminalType> productInfoTerminalTypeQueryWrapper = new QueryWrapper<>();
        productInfoTerminalTypeQueryWrapper.lambda().eq(ProductInfoTerminalType::getProductInfoCode, productInfo.getCode());
        this.productInfoTerminalTypeService.remove(productInfoTerminalTypeQueryWrapper);

        List<ProductInfoTerminalType> productInfoTerminalTypes = productInfo.getProductInfoTerminalTypes();
        for (ProductInfoTerminalType productInfoTerminalType : productInfoTerminalTypes) {
            productInfoTerminalType.setProductInfoCode(productInfo.getCode());
        }
        this.productInfoTerminalTypeService.saveBatch(productInfoTerminalTypes);

        //2.保存历史基本信息
        HisProductInfo hisProductInfo = new HisProductInfo();
        //将历史信息set最新信息
        BeanUtils.copyProperties(productInfo, hisProductInfo);
        //设置版本号
        Integer version = productInfo.getVersion();
        hisProductInfo.setVersion(version+1);
        this.hisProductInfoService.save(hisProductInfo);
        //终端信息
        ArrayList<HisProductInfoTerminalType> hisProductInfoTerminalTypes = new ArrayList<>();
        for (ProductInfoTerminalType productInfoTerminalType : productInfoTerminalTypes) {
            HisProductInfoTerminalType hisProductInfoTerminalType = new HisProductInfoTerminalType();
            BeanUtils.copyProperties(productInfoTerminalType, hisProductInfoTerminalType);
            hisProductInfoTerminalType.setHisProductInfoCode(hisProductInfo.getCode());
            hisProductInfoTerminalTypes.add(hisProductInfoTerminalType);//&
        }
        this.hisProductInfoTerminalTypeService.saveBatch(hisProductInfoTerminalTypes);
        //消息通知发送
        MessageNotificationProduct messageNotifiProduct = new MessageNotificationProduct();
        messageNotifiProduct.setSendTime(new Date());
        messageNotifiProduct.setFread(2);//未读
        messageNotifiProduct.setHandle(2);//未处理
        messageNotifiProduct.setType(1);//待审核
        messageNotifiProduct.setKind(1);//基本信息
        String title = productInfo.getName() + "的产品基本信息待审核信息";
        messageNotifiProduct.setTitle(title);
        String content = "领导,您有一个产品需要审核,大致如下:" + "产品名称" + productInfo.getName() + "请您审核";
        messageNotifiProduct.setContent(content);
        messageNotifiProduct.setHisProductInfoCode(hisProductInfo.getCode());
        this.messageNotificationProductService.save(messageNotifiProduct);
    }

    @Override
    public void publish(String code) {
        QueryWrapper<ProductInfo> productInfoQueryWrapper = new QueryWrapper<>();
        productInfoQueryWrapper.lambda().eq(ProductInfo::getCode,code);
        ProductInfo infoServiceOne = this.productInfoService.getOne(productInfoQueryWrapper);
        //维护状态
        infoServiceOne.setStatus(ProductInfo.Status.STATUS_PUBLISH);
        //修改
        this.productInfoService.updateById(infoServiceOne);

        //3.发送消息
        MessageNotificationProduct messageNotifiProduct = new MessageNotificationProduct();
        messageNotifiProduct.setSendTime(new Date());
        messageNotifiProduct.setFread(2);//未读
        messageNotifiProduct.setHandle(2);//未处理
        messageNotifiProduct.setType(1);//待审核
        messageNotifiProduct.setKind(1);//基本信息
        String title = infoServiceOne.getName() + "的产品基本信息待审核信息";
        messageNotifiProduct.setTitle(title);
        String content = "领导,您有一个产品需要审核,大致如下:" + "产品名称" + infoServiceOne.getName() + "请您审核";
        messageNotifiProduct.setContent(content);
        messageNotifiProduct.setHisProductInfoCode(code);
        this.messageNotificationProductService.save(messageNotifiProduct);

    }

    @Override
    public void abort(String code) {
        QueryWrapper<ProductInfo> productInfoQueryWrapper = new QueryWrapper<>();
        productInfoQueryWrapper.lambda().eq(ProductInfo::getCode,code);
        ProductInfo productInfoServiceOne = this.productInfoService.getOne(productInfoQueryWrapper);
        productInfoServiceOne.setStatus(ProductInfo.Status.STATUS_ABORT);
        productInfoServiceOne.setUpdateTime(new Date());//维护修改时间
        this.productInfoService.updateById(productInfoServiceOne);
    }

    @Override
    public Page<ProductInfo> findPage(Page<ProductInfo> page) {
        page=this.productInfoService.page(page);
        List<ProductInfo>  productInfos= page.getRecords();
        for (ProductInfo productInfo:productInfos){
            //获取code
            String code = productInfo.getCode();
            //终端类型
            QueryWrapper<ProductInfoTerminalType> productInfoTerminalTypeQueryWrapper = new QueryWrapper<>();
            //比较
            productInfoTerminalTypeQueryWrapper.lambda().eq(ProductInfoTerminalType::getProductInfoCode,code);
            //获取全部终端类型
            List<ProductInfoTerminalType> productInfoTerminalTypes = productInfoTerminalTypeService.list(productInfoTerminalTypeQueryWrapper);
            //赋值
            productInfo.setProductInfoTerminalTypes(productInfoTerminalTypes);


        }
        return page;
    }

    @Override
    public Page<ProductInfo> findPage(Integer current, Integer size) {
        Page<ProductInfo> page = new Page<>();
        page.setCurrent(current);
        page.setSize(size);

        return findPage(page);
    }

    @Override
    public ProductInfo detail(String code) {
        QueryWrapper<ProductInfo> productInfoQueryWrapper = new QueryWrapper<>();
        productInfoQueryWrapper.lambda().eq(ProductInfo::getCode,code);
        ProductInfo productInfo = this.productInfoService.getOne(productInfoQueryWrapper);
        return productInfo;
    }
}
